package com.doria.sso.utils;

import com.doria.common.ad_enum.CodeEnum;
import com.doria.common.ad_enum.MsgEnum;
import com.doria.common.pojo.vo.HttpResult;
import com.doria.common.pojo.vo.MyToken;
import com.doria.sso.config.SaltParamConfig;
import io.jsonwebtoken.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;

/**
 * token工具，token的成以及校验
 */
@Service
public class JwtUtils {

    private final SaltParamConfig saltParamConfig;

    public JwtUtils(SaltParamConfig saltParamConfig) {
        this.saltParamConfig = saltParamConfig;
    }


    /**
     * 根据id生成token
     *
     * @param id
     * @return
     */
    public HttpResult getToken(Long id) {
        MyToken myToken = this.creatToken(id.toString());
        return new HttpResult(true, CodeEnum.SUCCESS, MsgEnum.SUCCESS, myToken);
    }
    /**
     * 根据id生成token
     *
     * @param id
     * @return
     */
    public HttpResult getToken(String id) {
        MyToken myToken = this.creatToken(id);
        return new HttpResult(true, CodeEnum.SUCCESS, MsgEnum.SUCCESS, myToken);
    }

    /**
     * 解析token
     * @param token
     * @return
     */
    public HttpResult checkToken(String token) {
        try {
            HashMap<String, Object> map = this.parseToken(token);
            return new HttpResult(true, CodeEnum.SUCCESS,MsgEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult(false,CodeEnum.INVALID_TOKEN,MsgEnum.PERMISSIONS_ERROR);
        }
    }

    /**
     * 生成jwt-token
     *
     * @return
     */
    protected MyToken creatToken(String id) {
        JwtBuilder jwtBuilder = Jwts.builder()
                //设置id【唯一标识,也就是"jti":556】
                .setId(id.toString())
                //设置签发给谁【"sub":556】
                .setSubject("YiJia")
                //设置签发时间【"iat":xxxx】
                .setIssuedAt(new Date())
                //设置加密类型和加密盐【HS256,secretKey:xxxxxxx】
                .signWith(SignatureAlgorithm.HS256, saltParamConfig.getSalt())
                .setExpiration(new Date(System.currentTimeMillis()));
        return new MyToken(jwtBuilder.compact(), saltParamConfig.getExpTime());
    }

    /**
     * 解析token
     *
     * @return
     */
    protected HashMap<String, Object> parseToken(String token)throws Exception {
        Jws<Claims> claimsJws = Jwts.parser()
                //设置盐
                .setSigningKey(saltParamConfig.getSalt())
                .parseClaimsJws(token);
        //可以通过claimsJws拿到body以及head
        Claims body = claimsJws.getBody();
        Header header = claimsJws.getHeader();
        HashMap<String, Object> map = new HashMap<>();
        map.put("body", body);
        map.put("header", header);
        return map;
    }

    //public static void main(String[] args) {
    //    JwtBuilder jwtBuilder = Jwts.builder()
    //            //设置id【唯一标识,也就是"jti":556】
    //            .setId("556")
    //            //设置签发给谁【"sub":556】
    //            .setSubject("YiJia")
    //            //设置签发时间【"iat":xxxx】
    //            .setIssuedAt(new Date())
    //            //设置加密类型和加密盐【HS256,secretKey:xxxxxxx】
    //            .signWith(SignatureAlgorithm.HS256, "JKK&K2jhsld%ajbm4$lkz3")
    //            .setExpiration(new Date(System.currentTimeMillis()+1000));
    //    String token = jwtBuilder.compact();
    //    System.out.println("token:" + token);
    //
    //    Jws<Claims> claimsJws = Jwts.parser()
    //            .setSigningKey("JKK&K2jhsld%ajbm4$lkz3")
    //            .parseClaimsJws(token);
    //    Claims body = claimsJws.getBody();
    //    System.out.println(body.getId());
    //    System.out.println(body.getSubject());
    //}
}
